package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.AreaListAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.AreaListBean;
import com.hd.crdao.bean.EventBean;
import com.hd.crdao.bean.LocationCityBean;
import com.hd.crdao.bean.ReleaseCityBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/5/31 0031
 */
public class CityActivity extends BaseActivity {
    @BindView(R.id.address_head)
    ExpandHeadView mAddressHead;
    @BindView(R.id.lv_address)
    ListView mLvAddress;
    public AreaListBean mAreaListBean;
    public String mFromWhere;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address;    //复用布局
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initListener() {
        mAddressHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mLvAddress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AreaListBean.ListAreaBean listAreaBean = mAreaListBean.getListArea().get(position);
                String provinceName = getIntent().getStringExtra("provinceName");
                String cityName = listAreaBean.getName();
                if(TextUtils.equals("home",mFromWhere)){
                    EventBean eventBean = new EventBean();
                    eventBean.setCityName(cityName);
                    eventBean.setProvinceName(provinceName);
                    eventBean.setAreaid(listAreaBean.getId());
                    EventBus.getDefault().post(eventBean);
                }else if(TextUtils.equals("find",mFromWhere)){
                    //这个是传给找车页面城市定位
                    LocationCityBean locationCityBean = new LocationCityBean();
                    locationCityBean.setCityName(cityName);
                    EventBus.getDefault().post(locationCityBean);
                }else if(TextUtils.equals("register",mFromWhere)){
                    Intent i= new Intent(UIUtils.getContext(),AreaActivity.class);
                    i.putExtra("parentIds",listAreaBean.getId());
                    i.putExtra("provinceName",provinceName);
                    i.putExtra("cityName",cityName);
                    startActivity(i);
                }else if(TextUtils.equals("release",mFromWhere)){
                    ReleaseCityBean release = new ReleaseCityBean();
                    release.setCityName(cityName);
                    EventBus.getDefault().post(release);
                }
                finish();
            }
        });

    }

    @Override
    protected void initData() {
        String parentId = getIntent().getStringExtra("parentId");
        mFromWhere = getIntent().getStringExtra("fromWhere");
        try {
            OkHttpUtils.post().url(Constants.CITYURL).addParams("type", "2").addParams("parentId", parentId).addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    ALog.e("请求数据失败,给出提示用户检查网络");
                    MyToast.show("获取数据失败,请检查网络...");
                }

                @Override
                public void onResponse(String response, int id) {
                    Gson gson = new Gson();
                    mAreaListBean = gson.fromJson(response, AreaListBean.class);
                    mLvAddress.setAdapter(new AreaListAdapter(mAreaListBean.getListArea()));
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
